package com.example.sixtrainingdemo.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.GoodsListAdapter;
import com.example.sixtrainingdemo.adapter.GoodsPhotoAdapter;
import com.example.sixtrainingdemo.database.DBHelper;
import com.example.sixtrainingdemo.entity.Goods;
import com.example.sixtrainingdemo.entity.Photo;

import java.util.List;

public class ProductDetailsActivity extends AppCompatActivity implements GoodsPhotoAdapter.OnPhotoListener{

    private DBHelper mDBHelper; // 数据库帮助类
    private Goods mGoods; // 商品对象
    private ActivityResultLauncher mActivityResultLauncher;
    private List<Photo> mPhotoList;
    private RecyclerView mPhotoRecyclerView;
    private GoodsPhotoAdapter mGoodsPhotoAdapter;
    private GoodsListAdapter mRecyclerAdapter;
    private int mGoodsId;
    private List<Goods> mGoodsList;

    private Button mButton1;
    private Button mButton2;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acyivity_productdetails);
        mButton1 = findViewById(R.id.addCartButton);
        mButton2 = findViewById(R.id.buyNowButton);
        ImageView imageView = findViewById(R.id.pima);

        // 初始化数据库帮助类
        mDBHelper = DBHelper.getInstance(this);

        Intent intent = getIntent();
        mGoodsId = intent.getIntExtra("GOODS_ID", -1);
        if (mGoodsId != -1) {
            // 根据商品ID查询商品详情
            DBHelper dbHelper = DBHelper.getInstance(this);

            mGoods = dbHelper.selectAllGoods(mGoodsId);
            if (mGoodsId != -1 && mDBHelper != null) {
                List<Photo> photos = mDBHelper.selectAllPhotosByGoodsId(mGoodsId);

                if (photos != null && !photos.isEmpty()) {
                    Photo firstPhoto = photos.get(0);
                    byte[] photoData = firstPhoto.getData();
                    if (photoData != null) {
                        Bitmap bitmap = BitmapFactory.decodeByteArray(photoData, 0, photoData.length);
                        imageView.setImageBitmap(bitmap);
                    }
                }
            }
        }



        // 假设我们通过某种方式获取到商品的ID，例如从Intent中获取
        int goodsId = getIntent().getIntExtra("GOODS_ID", 0);

        // 根据商品ID查询商品数据
        mGoods = getGoodsById(goodsId);

        // 绑定数据到UI元素
        if (mGoods != null) {
            TextView titleTextView = findViewById(R.id.titleTextView);
            TextView priceTextView = findViewById(R.id.priceTextView);
//            ImageView pima = findViewById(R.id.pima);
//            ImageView imageView = findViewById(R.id.pima);


            // 设置文本和价格
//            titleTextView.setText(mGoods.getName());
//            priceTextView.setText(String.format("￥%.2f", mGoods.getPrice()));

            if (!TextUtils.isEmpty(mGoods.getName())) {
                titleTextView.setText(mGoods.getName());
            }
            if (mGoods.getPrice() > 0) {
                priceTextView.setText(String.format("￥%.2f", mGoods.getPrice()));
            }







        }



        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 假设 mGoods 是当前商品的实例
                DBHelper dbHelper = DBHelper.getInstance(ProductDetailsActivity.this);
                // 启动 ShopActivity
                Intent intent = new Intent(ProductDetailsActivity.this, ShopActivity.class);
                intent.putExtra("GOODS_ID", mGoodsId); // 传递商品ID，如果需要在ShopActivity中使用
                startActivity(intent);
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "请先加入购物车在购买哦！", Toast.LENGTH_SHORT).show();
            }
        });



    }



    @Override
    public void onAdd() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        mActivityResultLauncher.launch(intent);
    }

    @Override
    public void onDelete(int position) {

    }

    @Override
    public void onClick(View v) {

    }

    private Goods getGoodsById(int goodsId) {
//        Goods goods = mDBHelper.selectAllGoods(goodsId);
//        return goods;
        return DBHelper.getInstance(this).selectAllGoods(goodsId);
    }

}


